<?php

//use yii\web\View;

use yii\helpers\Url;
use common\models\Word;

?>
<?php echo $this->render("/layouts/top", ["loginUser" => $loginUser])?>

<style>

.questiondiv{
	margin:0px auto; 
	margin-top:1px; 
	width:1000px; 

	line-height:28px; 
	background-color:#EEE;
}

.questiondiv span{

	
}

ul{margin:0;padding:0;list-style:none;}  


.rightnum{
	display:inline-block;
	padding:0px;
	width:50px; 
	height:28px; 
	font-size:18px;
	text-align:center;
}


td, th {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: keep-all;
}


</style>
<script type="text/javascript" src="/js/NoLimitCategory.js"></script>
<div style="margin:0px auto; width:1000px; margin-bottom:10px;"><!--
--><div style="display:inline-block;">选择分类：</div><!--
--><div id="cont_category" style="display:inline-block; vertical-align:top">
  </div><!--
--><!--
-->
</div>
<?php echo $this->render("/module/correct-condition", ['correct'=>$correct])?>
<?php echo $this->render("/module/tags", ['tags'=>$tags])?>
<div style="margin:0px auto; width:1000px; margin-bottom:10px; font-size:14px">英文：
  <input id="searchtext" type="text" value="<?php echo $searchtext?>" style=" width:160px; height:20px;"/>
</div>
<div style="margin:0px auto; width:1000px; margin-bottom:10px; font-size:14px">翻译：
  <input id="transtext" type="text" value="<?php echo $transtext?>" style=" width:160px; height:20px;"/>
</div>
<div style="margin:0px auto; width:1000px; margin-bottom:10px;"><!--
--><div style="display:inline-block; margin-left:0px;"><input type="button" value="查找" onclick="search()" style=" width:50px; height:24px;"/></div>
</div>

<table align="center" cellpadding="0" cellspacing="1" style="width: 1000px;table-layout:fixed;">
    <tr bgcolor="#EEE" height="28">
        <td width="28" align="center"><input id="all_select_input" type="checkbox" value=""></td>
        <td width="50" align="center">序号</td>
        <td width="100">英文</td>
        <td width="600">中文</td>
        <td>标签</td>
        <td>顺序</td>
        <td align="center">操作</td>
    </tr>
    <tbody id="databody">
    <?php

    //print_r($english);
    for($i=0; $i<count($english); $i++){

        $junior = intval($english[$i]["junior"]);
        $senior = intval($english[$i]["senior"]);
        $cet4 = intval($english[$i]["cet4"]);
        $cet6 = intval($english[$i]["cet6"]);
        $ielts = intval($english[$i]["ielts"]);

        $tagstr = "";
        $tagstr .= ($junior==1) ? ",初" : "";
        $tagstr .= ($senior==1) ? ",高" : "";
        $tagstr .= ($cet4==1) ? ",四" : "";
        $tagstr .= ($cet6==1) ? ",六" : "";
        $tagstr .= ($ielts==1) ? ",雅" : "";

        $tagstr = preg_replace('/^,/', '', $tagstr);

        //行色
        $trColor = ($i%2==1) ? '#EEE' : '#FEFEFE';

        ?>
        <tr bgcolor="<?php echo $trColor?>" height="28" eid="<?php echo $english[$i]["id"]?>">
            <td width="28" align="center"><input type="checkbox" value="<?php echo $english[$i]["id"]?>"></td>
            <td width="30" align="center"><?php echo $english[$i]["id"]?></td>
            <td width="100"><?php echo $english[$i]["word"]?></td>
            <td title="<?php echo $english[$i]["trans"]?>"><?php echo $english[$i]["trans"]?></td>
            <td title="<?php echo $tagstr?>"><?php echo $tagstr?></td>
            <td><?php echo $english[$i]["displayorder"]?></td>
            <td align="center"><input type="button" value="编辑" onclick="edit(<?php echo $english[$i]["id"]?>)"/></td>
        </tr>

        <?php
    }
    ?>
    </tbody>
</table>
<br>
<div style="margin: 0px auto;width:1000px; font-size: 0px;">
    <div style="display: inline-block;"><input type="button" value="相同顺序" onclick="order_same()"/></div>
    <div style="display: inline-block; margin-left: 10px;"><input type="button" value="顺序放后" onclick="order_last()"/></div>
    <div style="display: inline-block; margin-left: 10px;"><input type="button" value="加至专题" onclick="addtotopic()"/></div>
</div>

<br>

<?php
	$pageurl = "?c1=$c1&c2=$c2&c3=$c3&cid=$cid&searchtext=$searchtext&transtext=$transtext&correct=$correct&tags=$tags&page=";
	
?>
<div style=" margin:0px auto; width:1000px; height:28px; line-height:28px; background-color:#EEE; text-align:center">
<span>总数据:<?php echo $pv->dataCount?>条</span>
<a href="<?php echo $pageurl."1"?>">首页</a>
<a href="<?php echo $pageurl.$pv->prevPage?>">上一页</a>
<span><?php echo $pv->currentPage."/".$pv->pageCount?></span>
<a href="<?php echo $pageurl.$pv->nextPage?>">下一页</a>
<a href="<?php echo $pageurl.$pv->pageCount?>">末页</a>
</div>

<script language="javascript">

var page = <?php echo $pv->currentPage;?>;
var c1 = <?php echo $c1?>;
var c2 = <?php echo $c2?>;
var c3 = <?php echo $c3?>;

var nlc = new NoLimitCategory(<?php echo json_encode($treeData, JSON_UNESCAPED_UNICODE)?>, document.getElementById("cont_category"), "全部");

nlc.onSelectCreate = function(selectObj){
	selectObj.style.cssText = 'margin-left:5px;height:24px;';
};


var cids = [];
if(c1 != 0)cids.push(c1);
if(c2 != 0)cids.push(c2);
if(c3 != 0)cids.push(c3);


nlc.append(cids);



function order_same() {

    let ids = [];

    $('#databody').children('tr').each(function (index, elem) {
        if($(this).find('input[type=checkbox]').is(':checked')){
            ids.push($(this).attr('eid'));
        }
    });

    $.ajax({
        url : "<?php echo Url::to(['english/order-same'])?>",
        type : 'post',
        data : {
            ids : ids,
        },
        dataType : 'json',
        success : function (result) {
            console.log(result);
            if(result.code == 200){
                alert("成功");
                //window.location.reload();
            }else{
                alert(result.info);
            }
        }
    });

}

function order_last() {

    let ids = [];

    $('#databody').children('tr').each(function (index, elem) {
        if($(this).find('input[type=checkbox]').is(':checked')){
            ids.push($(this).attr('eid'));
        }
    });

    $.ajax({
        url : "<?php echo Url::to(['english/order-last'])?>",
        type : 'post',
        data : {
            ids : ids,
        },
        dataType : 'json',
        success : function (result) {
            console.log(result);
            if(result.code == 200){
                window.location.reload();
            }else{
                alert(result.info);
            }
        }
    });

}


function addtotopic(){

	var checkboxs  = $('.questiondiv input:checkbox');

	//获得选中的数据
	var ids = '';
	for(var i=0; i<checkboxs.length; i++){
		if($(checkboxs[i]).is(':checked')){
			console.log(checkboxs[i]);
			ids += ','+$(checkboxs[i]).val();
		}
	}
	ids = ids.length>0 ? ids.substring(1) : '';

//	console.log(ids);

	if(ids.length  >0){
		window.open('<?php echo Url::to(['topic/edit'])?>'+'?ids='+ids);
	}else{
		alert("请选择要提交的数据");
	}


}


function search(){
	var searchtext = $('#searchtext').val();
	var transtext = $('#transtext').val();
	var categorys = nlc.getSelects();
	var correct = getCorrectParam();
	var tags = getTagsParam();

	var c1=0, c2=0, c3=0, cid = 0;
	for(var i=0; i<categorys.length; i++){
		if(i == 0)c1 = categorys[i].value;
		if(i == 1)c2 = categorys[i].value;
		if(i == 2)c3 = categorys[i].value;
		if(categorys[i].value > 0)cid = categorys[i].value;//所属分类为不为0的最后一个
	}

	var pageurl = "?"+"c1="+c1+"&c2="+c2+"&c3="+c3+"&cid="+cid+"&searchtext="+encodeURIComponent(searchtext)+"&transtext="+encodeURIComponent(transtext)
	+"&correct="+correct+"&tags="+tags+"&page="+page;

	window.location.href = pageurl;

}

function edit(wid){
	window.location.href = "<?php echo Url::to(['/english/edit'])?>"+"?id="+wid;
}

$(document).ready(function(){

    //全选
    $('#all_select_input').on('click', function () {
        if($(this).is(':checked')){
            $('#databody').find('input[type=checkbox]').prop("checked",true);
        }else{
            $('#databody').find('input[type=checkbox]').prop("checked",false);
        }
    });

});

</script>
<br>
<?php echo $this->render("/layouts/footer")?>
